/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.controller;

import com.solid4j.auth.common.Constants;
import com.solid4j.auth.model.Privilege;
import com.solid4j.auth.model.Role;
import com.solid4j.auth.page.Page;
import com.solid4j.auth.service.PrivilegeService;
import com.solid4j.auth.service.RoleService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: chengfeng
 * @date: 17/4/18
 */
@Controller
@RequestMapping("/auth/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private PrivilegeService privilegeService;

    @RequestMapping("/list")
    public String list(Role role, @RequestParam(defaultValue = "1", required = true) Integer currentPage,
                       Model model) {
        String name = role.getRoleName();
        StringBuilder condition = new StringBuilder("");
        if(!StringUtils.isEmpty(name)) {
            condition.append("role_name like '%" + name.trim() + "%'");
        }
        Page<Role> page = roleService.getRoleList(condition.toString(), currentPage, Constants.PAGE_SIZE, "");
        model.addAttribute("page", page);
        return "/role/list";
    }

    @RequestMapping("/redirect")
    public String redirect(@RequestParam(defaultValue = "save", required = true) String type, @RequestParam(value = "id", required = false) Integer id, Model model) {
        if ("save".equals(type)) {
            List<Privilege> privilegeList = privilegeService.getPrivileges();
            model.addAttribute("privilegeList", privilegeList);
            return "/role/save";
        } else if ("update".equals(type)) {
            Role role = roleService.get(id);
            List<Privilege> privileges = privilegeService.getPrivileges();
            Map<Privilege, Integer> privilegeMap = new LinkedHashMap<Privilege, Integer>();
            Map<Integer, Integer> checkedMap = new LinkedHashMap<Integer, Integer>();
            if (privileges != null && !privileges.isEmpty()) {
                for (Privilege privilege : privileges) {
                    privilegeMap.put(privilege, 0);
                }
            }
            if (role.getPrivileges() != null && !role.getPrivileges().isEmpty()) {
                for (Privilege privilege : role.getPrivileges()) {
                    checkedMap.put(privilege.getId(), 1);
                }
            }
            model.addAttribute("role", role);
            model.addAttribute("privilegeMap", privilegeMap);
            model.addAttribute("checkedMap", checkedMap);
            return "/role/update";
        }
        return null;
    }

    @RequestMapping(value = "/save")
    public String save(@ModelAttribute Role role, @RequestParam(value = "", defaultValue = "") String privilegeIds) {
        Set<Privilege> privileges = new LinkedHashSet<Privilege>();
        role.setCreateTime(new Date());
        role.setModifyTime(new Date());
        if (!"".equals(privilegeIds)) {
            String[] privilegeIdArr = privilegeIds.split(",");
            for (String privilegeId : privilegeIdArr) {
                privileges.add(privilegeService.get(Integer.parseInt(privilegeId)));
            }
        }
        role.setPrivileges(privileges);
        roleService.save(role);
        return "redirect:/auth/role/list.do?currentPage=1";
    }

    @RequestMapping(value = "/delete")
    public String delete(@RequestParam(value = "id") Integer id, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        Role role = roleService.get(id);
        roleService.delete(role);
        return "redirect:/auth/role/list.do?currentPage=" + currentPage;
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute Role role, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "", defaultValue = "") String privilegeIds) {
        Role newRole = roleService.get(role.getId());
        Set<Privilege> privileges = new LinkedHashSet<Privilege>();
        newRole.setRoleName(role.getRoleName());
        newRole.setRoleDesc(role.getRoleDesc());
        newRole.setModifyTime(new Date());
        if (!"".equals(privilegeIds)) {
            String[] privilegeIdArr = privilegeIds.split(",");
            for (String privilegeId : privilegeIdArr) {
                privileges.add(privilegeService.get(Integer.parseInt(privilegeId)));
            }
        }
        newRole.setPrivileges(privileges);
        roleService.update(newRole);
        return "redirect:/auth/role/list.do?currentPage=" + currentPage;
    }

}
